﻿using System;
using System.Text;

namespace wojilu
{
    /// <summary>
    /// Random类
    /// </summary>
    public static class Rand
    {
        private static Random rnd = new Random();

        #region 根据用户指定的字符串和指定长度生成随机的字符串
        /// <summary>
        /// 根据用户指定的字符串和指定长度生成随机的字符串
        /// </summary>
        /// <param name="pwdchars"></param>
        /// <param name="pwdlen"></param>
        /// <returns></returns>
        public static string MakeRandomString(this string pwdchars, int pwdlen)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < pwdlen; i++)
            {
                int num = rnd.Next(pwdchars.Length);
                builder.Append(pwdchars[num]);
            }
            return builder.ToString();
        }
        #endregion

        #region 生成0-9随机数
        /// <summary>
        /// 生成0-9随机数
        /// </summary>
        /// <param name="VcodeNum">生成长度</param>
        /// <returns></returns>
        public static string RndNum(int VcodeNum)
        {
            StringBuilder sb = new StringBuilder(VcodeNum);
            Random rand = new Random();
            for (int i = 1; i < VcodeNum + 1; i++)
            {
                int t = rand.Next(9);
                sb.AppendFormat("{0}", t);
            }
            return sb.ToString();

        }
        #endregion

        public static string RandomString(int lens)
        {
            char[] chArray = new char[] { 
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
        'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
        'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
     };
            int length = chArray.Length;
            string str = "";
            Random random = new Random();
            for (int i = 0; i < lens; i++)
            {
                str = str + chArray[random.Next(length)];
            }
            return str;
        }

        #region 生成随机字符串
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="int_NumberLength">字符长度</param>
        /// <returns></returns>
        public static string GetRandomNumberString(int int_NumberLength)
        {
            return GetRandomNumberString(int_NumberLength, false);
        }
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="int_NumberLength">数字长度</param>
        /// <param name="onlyNumber">是否纯数字</param>
        /// <returns></returns>
        public static string GetRandomNumberString(int int_NumberLength, bool onlyNumber)
        {
            Random random = new Random();
            return GetRandomNumberString(int_NumberLength, onlyNumber, random);
        }
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="int_NumberLength">数字长度</param>
        /// <param name="onlyNumber">是否纯数字</param>
        /// <param name="random">随机生成器</param>
        /// <returns></returns>
        public static string GetRandomNumberString(int int_NumberLength, bool onlyNumber, Random random)
        {
            string strings = "123456789";
            if (!onlyNumber) strings += "abcdefghjkmnpqrstuvwxyz";
            char[] chars = strings.ToCharArray();
            string returnCode = string.Empty;
            for (int i = 0; i < int_NumberLength; i++)
                returnCode += chars[random.Next(0, chars.Length)].ToString();
            return returnCode;
        }
        #endregion

        public static string GetFileName()
        {
            var random = new Random();
            var builder = new StringBuilder();
            builder.Append(DateTime.Now.ToString("yyyyMMddHHmmss"));
            builder.Append(random.Next(0x186a0, 0xf423f).ToString());
            return builder.ToString();
        }

    }

}
